﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;

namespace MiniTalk.Net
{
    internal class NetDatabase
    {
        static string connectionString = "server=tavin.cn;database=localchat;user=localchat;password=LyAXFzN7LXaaGeck;";

        public static void InitializeDatabase()
        {
            using (MySqlConnection connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                string createTableQuery = @"
                    CREATE TABLE IF NOT EXISTS Messages (
                        Id INT AUTO_INCREMENT PRIMARY KEY,
                        Username VARCHAR(100) NOT NULL,
                        Content VARCHAR(500) NOT NULL,
                        Timestamp DATETIME NOT NULL
                    );";

                using (MySqlCommand command = new MySqlCommand(createTableQuery, connection))
                {
                    command.ExecuteNonQuery();
                }
            }
        }

        public static void RecordMessageToDatabase(string username, string message)
        {
            using (MySqlConnection connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                string query = "INSERT INTO Messages (Username, Content, Timestamp) VALUES (@Username, @Content, @Timestamp)";

                using (MySqlCommand command = new MySqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@Username", username);
                    command.Parameters.AddWithValue("@Content", message);
                    command.Parameters.AddWithValue("@Timestamp", DateTime.Now);
                    command.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// 获取历史消息记录的方法。
        /// 可以根据实际需要进行筛选，如获取最近100条记录。
        /// </summary>
        public static List<HistoryMessage> GetHistoryMessages(int limit = 50)
        {
            List<HistoryMessage> messages = new List<HistoryMessage>();
            using (MySqlConnection connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                string query = "SELECT Username, Content, Timestamp FROM Messages ORDER BY Timestamp DESC LIMIT @Limit";


                using (MySqlCommand command = new MySqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@Limit", limit);
                    using (MySqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string username = reader.GetString("Username");
                            string content = reader.GetString("Content");
                            DateTime timestamp = reader.GetDateTime("Timestamp");
                            messages.Add(new HistoryMessage
                            {
                                Username = username,
                                Content = content,
                                Timestamp = timestamp
                            });
                        }
                    }
                }
            }
            // 倒序查询后，可视情况反转列表，以使最早的消息先显示
            messages.Reverse();
            return messages;
        }
    }

    /// <summary>
    /// 用于存储查询到的历史消息记录的数据结构
    /// </summary>
    internal class HistoryMessage
    {
        public string Username { get; set; }
        public string Content { get; set; }
        public DateTime Timestamp { get; set; }
    }
}
